package cn.e4j.modular.api.controller;


import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.api.service.Gwaq553Service;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuhuan on 2018/08/15.
 */
@RestController
@RequestMapping("api/wx/gwaq553")
public class Gwaq553Controller {

    private static Logger logger = LoggerFactory.getLogger(Gwaq553Controller.class);

    @Autowired
    private Gwaq553Service gwaq553Service;

    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    GwbService gwbService;

    /**
     * 获取商机信息列表
     * @return
     */
    @RequestMapping(value = "getGwaq553List")
    @ResponseBody
    public ResponseData getServiceOrderList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        List<Dto> list = new ArrayList<>();
        try {
            inDto.put("GWBHENT", inDto.getString("ENT"));
            //inDto.put("gwbhacti", "Y");
            inDto.put("GWBH002", currentUser.getID());

            //排序
//            Dto sort = new Dto();
//            sort.put("property", "GWBH003");
//            sort.put("direction", "desc");
//            sortList.add(sort);

            //客户权限
            Dto gwb001 = gwbService.getGwb001(currentUser,inDto);
            inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            inDto.put("ORGS", sys003.get("ORGS"));

            //业务员权限
            Dto sys004 = systemService.getSys004(currentUser,(List<Dto>)sys003.get("ORGS"));
            inDto.put("YWY", sys004.get("YWY"));

//            inDto.put("sort", AppJson.toGridJson(sortList));
            list = InvokeUtil.invokeBeanMethod("gwaq553Mapper,getGwaq553ListPage",wapper.getPage(), inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData(list);
    }

    /**
     * 获取该商机主题对应的经销商列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "jxsList")
    public ResponseData jxsList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwaq553Service.jxsList(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 获取经销商
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getjxsList")
    public ResponseData getjxsList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("GWBHENT",inDto.getString("ENT"));
        List<Dto> list = gwaq553Service.getjxsList(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 获取商品明细
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getspmxList")
    public ResponseData getspmxList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("GWBKENT",inDto.getString("ENT"));
        List<Dto> list = gwaq553Service.getspmxList(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 获取竞争信息列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getJzxxPage")
    public ResponseData getJzxxPage(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwaq553Service.getJzxxPage(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 根据主题+ 客户获取 评估值
     *
     * @return
     * @author wenchuan.li on 2018/07/12 15:52
     */
    @RequestMapping(value = "getPgx")
    @ResponseBody
    public ResponseData getPgx(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        try {
            inDto.put("GWBLENT", inDto.getString("ENT"));
            inDto.put("GWBLACTI", "Y");

            list = gwaq553Service.getPgx(inDto);
        } catch (Exception e) {
            e.printStackTrace();
           return new ErrorResponseData();
        }
        return new SuccessResponseData(list);
    }


    /**
     * 获取拜访历程 列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getBflcPage")
    public ResponseData getBflcPage(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwaq553Service.getBflcPage(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 根据成交率分类（A\B\C）统计商机明细数量
     *
     * @return
     * @author wenchuan.li on 2018/11/21 17:40
     */
    @RequestMapping(value = "statsSJ")
    @ResponseBody
    public ResponseData statsSJ(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        List<Dto> list = new ArrayList<>();
        try {

            inDto.put("GWBHENT", inDto.getString("ENT"));
            inDto.put("GWBHACTI", "Y");
            inDto.put("GWBH002", currentUser.getID());

            //客户权限
            Dto gwb001 = gwbService.getGwb001(currentUser,inDto);
            inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            inDto.put("ORGS", sys003.get("ORGS"));

            list = gwaq553Service.statsSJ(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData(list);
    }


}
